home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_223 / csh / execom.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  19KB  |  749 lines

  1. /*
  2.  * EXECOM.C
  3.  *
  4.  * Matthew Dillon, 10 August 1986
  5.  *    Finally re-written.
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  *
  9.  * Version 3.03A by Carlo Borreo & Cesare Dieni 12-May-89
  10.  *
  11.  */
  12.  
  13. extern char *v_histnum, *v_except;
  14.  
  15. #define F_EXACT 0
  16. #define F_ABBR  1
  17.  
  18. #define ST_COND   0x01
  19. #define ST_NORED  0x02
  20. #define ST_NOEXP  0x04
  21. #define ST_AV     0x08 /* delimit args within a variable */
  22.  
  23. int has_wild = 0;                 /* set if any arg has wild card */
  24.  
  25. struct COMMAND {
  26.     int (*func)();
  27.     short minargs;
  28.     short stat;
  29.     int val;
  30.     char *name;
  31. };
  32.  
  33. extern char *format_insert_string();
  34. extern char *mpush(), *exarg();
  35.  
  36. extern int do_fltupper(), do_fltlower();
  37. extern int do_strleft(), do_strright(), do_strmid(), do_strlen();
  38. extern int do_fornum(), do_forline(), do_exec();
  39. extern int do_diskchange(), do_stack(), do_fault(), do_path(), do_pri();
  40. extern int do_rpn(), do_resident(), do_truerun(), do_aset(), do_howmany();
  41. extern int do_open(), do_close(), do_fileslist(), do_htype();
  42. extern int do_run(), do_number(), do_assign(), do_join();
  43. extern int do_quit(), do_set_var(), do_unset_var();
  44. extern int do_echo(), do_source(), do_mv(), do_addbuffers();
  45. extern int do_cd(), do_pwd(), do_rm(), do_mkdir(), do_history();
  46. extern int do_mem(), do_cat(), do_dir(), do_info(), do_inc();
  47. extern int do_foreach(), do_return(), do_if(), do_label(), do_goto();
  48. extern int do_input(), do_ver(), do_sleep(), do_help();
  49. extern int do_strhead(), do_strtail(), do_relabel();
  50. extern int do_copy(), date(), do_protect(), do_ps();
  51. extern int do_forever(), do_abortline(), do_strings(), do_touch();
  52. extern int do_window(), do_search(), do_filenote();
  53. char *push_cpy();
  54.  
  55. static struct COMMAND Command[] = {
  56. do_run,        0, ST_AV,    0,    "\001",   /* may call do_source */
  57. do_set_var,    0, 0, LEVEL_ALIAS,    "alias",  /* uses avline */
  58. do_abortline,    0, 0,        0,    "abortline",
  59. do_addbuffers,    2, 0,        0,    "addbuffers",
  60. do_aset,    1, 0,        0,    "aset",
  61. do_assign,    0, 0,        0,    "assign",
  62. do_cat,        0, 0,        0,    "cat",
  63. do_cd,        0, 0,        0,    "cd",
  64. do_close,    0, 0,        0,    "close",
  65. do_copy,    1, 0,        0,    "copy",
  66. do_copy,    1, 0,        0,    "cp",
  67. date,        0, 0,        0,    "date",
  68. do_rm,        0, 0,        0,    "delete",
  69. do_dir,        0, ST_NOEXP,    0,    "dir",
  70. do_diskchange,    1, 0,        0,    "diskchange",
  71. do_inc,        1, 0,        -1,    "dec",
  72. do_echo,    0, 0,        0,    "echo", /* uses avline */
  73. do_if,        0, ST_COND,    1,    "else",
  74. do_if,        0, ST_COND,    2,    "endif",
  75. do_exec,    1, 0,        0,    "exec",
  76. do_fault,    1, 0,        0,    "fault",
  77. do_filenote,    2, 0,        0,    "filenote",
  78. do_fileslist,    0, 0,        0,    "flist",
  79. do_fltlower,    0, 0,        0,    "fltlower",
  80. do_fltupper,    0, 0,        0,    "fltupper",
  81. do_foreach,    3, ST_NORED,    0,    "foreach",
  82. do_forever,    1, ST_NORED,    0,    "forever",
  83. do_forline,    3, ST_NORED,    0,    "forline",
  84. do_fornum,    4, ST_NORED,    0,    "fornum",
  85. do_goto,    1, 0,        0,    "goto",
  86. do_help,    0, 0,        0,    "help",
  87. do_history,    0, 0,        0,    "history",
  88. do_howmany,    0, 0,        0,    "howmany",
  89. do_htype,    1, 0,        0,    "htype",
  90. do_if,        1, ST_COND|ST_NORED,0,    "if",
  91. do_inc,        1, 0,        1,    "inc",
  92. do_info,    0, 0,        0,    "info",
  93. do_join,    2, 0,        1,    "join",
  94. do_input,    1, 0,        0,    "input",
  95. do_label,    1, ST_COND,    0,    "label",
  96. do_dir,        0, ST_NOEXP,    0,    "ls",
  97. do_mkdir,    0, 0,        0,    "md",
  98. do_mem,        0, 0,        0,    "mem",
  99. do_mkdir,    0, 0,        0,    "mkdir",
  100. do_mv,        2, 0,        0,    "mv",
  101. do_open,    3, 0,        0,    "open",
  102. do_path,    0, 0,        0,    "path",
  103. do_pri,        2, 0,        0,    "pri",
  104. do_protect,    2, 0,        0,    "protect",
  105. do_ps,        0, 0,        0,    "ps",
  106. do_pwd,        0, 0,        0,    "pwd",
  107. do_quit,    0, ST_NORED,    0,    "quit",
  108. do_truerun,    1, ST_NORED,    1,    "rback",
  109. do_mv,        2, 0,        0,    "rename",
  110. do_relabel,    2, 0,        0,    "relabel",
  111. do_resident,    0, 0,        0,    "resident",
  112. do_return,    0, 0,        0,    "return",
  113. do_rm,        0, 0,        0,    "rm",
  114. do_rpn,        0, ST_NOEXP|ST_NORED,0,    "rpn",
  115. do_truerun,    1, ST_NORED,    0,    "run",
  116. do_search,    2, 0,        0,    "search",
  117. do_set_var,    0, ST_AV, LEVEL_SET,    "set",
  118. do_sleep,    0, 0,        0,    "sleep",
  119. do_source,    0, ST_NORED|ST_AV, 0,    "source", /* uses avline */
  120. do_stack,    0, 0,        0,    "stack",
  121. do_strhead,    3, 0,        0,    "strhead",
  122. do_strings,    1, 0,        0,    "strings",
  123. do_strleft,    3, 0,        0,    "strleft",
  124. do_strlen,    2, 0,        0,    "strlen",
  125. do_strmid,    3, 0,        0,    "strmid",
  126. do_strright,    3, 0,        0,    "strright",
  127. do_strtail,    3, 0,        0,    "strtail",
  128. do_touch,    0, 0,        0,    "touch",
  129. do_cat,        0, 0,        0,    "type",
  130. do_unset_var,    0, 0, LEVEL_ALIAS,    "unalias",
  131. do_unset_var,    0, 0, LEVEL_SET  ,    "unset",
  132. do_ver,        0, 0,        0,    "version",
  133. do_window,    0, ST_NOEXP,    0,    "window",
  134. '\0',        0, 0,        0,    NULL
  135. };
  136.  
  137. static unsigned char elast;        /* last end delimeter */
  138. static char Cin_ispipe, Cout_ispipe;
  139.  
  140. exec_command(base)
  141. char *base;
  142. {
  143. register char *scr;
  144. char buf[32];
  145.  
  146. if (!H_stack) {
  147.     add_history(base);
  148.     sprintf(buf, "%d", H_tail_base + H_len);
  149.     set_var(LEVEL_SET, v_histnum, buf);
  150.     }
  151. scr = malloc((strlen(base) << 2) + 2);
  152. preformat(base, scr);
  153. return (fcomm(scr, 1) ? -1 : 1);
  154. }
  155.  
  156. isalphanum(c)
  157. register char c;
  158. {
  159. return (
  160.     (c >= '0' && c <= '9') ||
  161.     (c >= 'a' && c <= 'z') ||
  162.     (c >= 'A' && c <= 'Z') ||
  163.     (c == '_')
  164.     );
  165. }
  166.  
  167. preformat(s, d)
  168. register char *s, *d;
  169. {
  170. register int si, di, qm;
  171.  
  172. si = di = qm = 0;
  173. while (s[si] == ' ' || s[si] == 9) ++si;
  174. while (s[si]) {
  175.     if (qm && s[si] != '\"' && s[si] != '\\') {
  176.         d[di++] = s[si++] | 0x80;
  177.         continue;
  178.         }
  179.     switch (s[si]) {
  180.         case ' ':
  181.         case 9:
  182.             d[di++] = ' ';
  183.             while (s[si] == ' ' || s[si] == 9) ++si;
  184.             if (s[si] == 0 || s[si] == '|' || s[si] == ';') --di;
  185.             break;
  186.         case '*':
  187.         case '?':
  188.             d[di++] = 0x80;
  189.         case '!':
  190.             d[di++] = s[si++];
  191.             break;
  192.         case '#':
  193.             d[di++] = '\0';
  194.             while (s[si]) ++si;
  195.             break;
  196.         case ';':
  197.         case '|':
  198.             d[di++] = s[si++];
  199.             while (s[si] == ' ' || s[si] == 9) ++si;
  200.             break;
  201.         case '\\':
  202.             d[di++] = s[++si] | 0x80;
  203.             if (s[si]) ++si;
  204.             break;
  205.         case '\"':
  206.             qm = 1 - qm;
  207.             ++si;
  208.             break;
  209.         case '^':
  210.             d[di++] = s[++si] & 0x1F;
  211.             if (s[si]) ++si;
  212.             break;
  213.         case '$': /* search end of var name and place false space */
  214.             d[di++] = 0x80;
  215.             d[di++] = s[si++];
  216.             while (isalphanum(s[si])) d[di++] = s[si++];
  217.             d[di++] = 0x80;
  218.             break;
  219.         default:
  220.             d[di++] = s[si++];
  221.             break;
  222.         }
  223.     }
  224. d[di++]=0;
  225. d[di]=0;
  226. if (debug) fprintf (stderr,"PREFORMAT: %d :%s:\n", strlen(d), d);
  227. }
  228.  
  229. extern BPTR extOpen();
  230.  
  231. /*
  232.  * process formatted string.  ' ' is the delimeter.
  233.  *
  234.  *    0: check '\0': no more, stop, done.
  235.  *    1: check $.     if so, extract, format, insert
  236.  *    2: check alias. if so, extract, format, insert. goto 1
  237.  *    3: check history or substitution, extract, format, insert. goto 1
  238.  *
  239.  *    4: assume first element now internal or disk based command.
  240.  *
  241.  *    5: extract each ' ' or 0x80 delimited argument and process, placing
  242.  *       in av[] list (except 0x80 args appended).  check in order:
  243.  *
  244.  *             '$'         insert string straight
  245.  *             '>'         setup stdout
  246.  *             '>>'        setup stdout flag for append
  247.  *             '<'         setup stdin
  248.  *             '*' or '?'  do directory search and insert as separate args.
  249.  *
  250.  *             ';' 0 '|'   end of command.  if '|' setup stdout
  251.  *                          -execute command, fix stdin and out (|) sets
  252.  *                           up stdin for next guy.
  253.  */
  254.  
  255.  
  256. fcomm(str, freeok)
  257. register char *str;
  258. {
  259.    static int alias_count;
  260.    int p_alias_count = 0;
  261.    char *istr;
  262.    char *nextstr;
  263.    char *command;
  264.    char *pend_alias = NULL;
  265.    char err = 0;
  266.    has_wild = 0;
  267.  
  268.    ++alias_count;
  269.  
  270.    mpush_base();
  271.    if (*str == 0)
  272.       goto done1;
  273. step1:
  274.    if (alias_count == MAXALIAS || ++p_alias_count == MAXALIAS) {
  275.       fprintf(stderr,"Alias Loop\n");
  276.       err = 20;
  277.       goto done1;
  278.    }
  279. /*
  280.    if (str[1] == '$') {
  281.       if (istr = get_var (LEVEL_SET, str + 2))
  282.          str = format_insert_string(str, istr, &freeok);
  283.    }
  284. */
  285.    istr = NULL;
  286.    if (*(unsigned char *)str < 0x80)
  287.       istr = get_var (LEVEL_ALIAS, str);  /* only if not \command */
  288.    *str &= 0x7F;                          /* remove \ teltail     */
  289.    if (istr) {
  290.       if (*istr == '%') {
  291.          pend_alias = istr;
  292.       } else {
  293.          str = format_insert_string(str, istr, &freeok);
  294.          goto step1;
  295.       }
  296.    }
  297.    if (*str == '!') {
  298.       char *p, c;                     /* fix to allow !cmd1;!cmd2 */
  299.       for(p = str; *p && *p != ';' ; ++p);
  300.       c = *p;
  301.       *p = '\0';
  302.       istr = get_history(str);
  303.       *p = c;
  304.       replace_head(istr);
  305.       str = format_insert_string(str, istr, &freeok);
  306.       goto step1;
  307.    }
  308.    nextstr = str;
  309.    command = exarg(&nextstr);
  310.    if (*command == 0)
  311.       goto done0;
  312.    if (pend_alias == 0) {
  313.       if (cmd_stat(command) & ST_COND)
  314.          goto skipgood;
  315.    }
  316.    if (disable || forward_goto) {
  317.       while (elast && elast != ';' && elast != '|')
  318.          exarg(&nextstr);
  319.       goto done0;
  320.    }
  321. skipgood:
  322.    {
  323.       register char *arg, *ptr, *scr;
  324.       short redir;
  325.       short doexpand;
  326.       short cont;
  327.       short inc;
  328.  
  329.       ac = 1;
  330.       av[0] = command;
  331. step5:                                          /* ac = nextac */
  332.       if (!elast || elast == ';' || elast == '|')
  333.          goto stepdone;
  334.  
  335.       av[ac] = '\0';
  336.       cont = 1;
  337.       doexpand = redir = inc = 0;
  338.  
  339.       while (cont && elast) {
  340.          int cstat = cmd_stat(command);
  341.  
  342.          ptr = exarg(&nextstr);
  343.          inc = 1;
  344.          arg = "";
  345.          cont = (elast == 0x80);
  346.          switch (*ptr) {
  347.          case '<':
  348.             redir = -2;
  349.          case '>':
  350.             if (cstat & (ST_NORED | ST_COND)) {
  351.                                                         /* don't extract   */
  352.                 redir = 0;                              /* <> stuff if its */
  353.                 arg = ptr;                              /* external cmd.   */
  354.                 break;
  355.             }
  356.             ++redir;
  357.             arg = ptr + 1;
  358.             if (*arg == '>') {
  359.                redir = 2;        /* append >> */
  360.                ++arg;
  361.             }
  362.             cont = 1;
  363.             break;
  364.          case '$':
  365.             /* restore args if from set command or pend_alias */
  366.             if ((arg = get_var(LEVEL_SET, ptr + 1)) != NULL) {
  367.                if (cstat & ST_COND) {
  368.                   char *tp;
  369.                   tp = push_cpy(arg);
  370.                   arg = tp;
  371.                }
  372.                else {
  373.                   char *pe, sv;
  374.                   while (pe = index(arg,0xA0)) {
  375.                      sv = *pe;
  376.                      *pe = '\0';
  377.                      av[ac++] = push_cpy(arg);
  378.                      *pe = sv;
  379.                      av[ac] = '\0';
  380.                      arg = pe+1;
  381.                   }
  382.                }
  383.             }
  384.             else
  385.                arg = ptr;
  386.             break;
  387.          case '*':
  388.          case '?':
  389.             if ((cstat & ST_NOEXP) == 0)
  390.                doexpand = 1;
  391.             arg = ptr;
  392.             break;
  393.          default:
  394.             arg = ptr;
  395.             break;
  396.          }
  397.  
  398.          /* Append arg to av[ac] */
  399.  
  400.          for (scr = arg; *scr; ++scr)
  401.             *scr &= 0x7F;
  402.          if (av[ac]) {
  403.             register char *old = av[ac];
  404.             av[ac] = mpush(strlen(arg)+strlen(av[ac]));
  405.             strcpy(av[ac], old);
  406.             strcat(av[ac], arg);
  407.          } else {
  408.             av[ac] = push_cpy(arg);
  409.          }
  410.          if (elast != 0x80)
  411.             break;
  412.       }
  413.  
  414.       /* process expansion */
  415.  
  416.       if (doexpand) {
  417.          char **eav, **ebase;
  418.          int eac;
  419.          has_wild = 1;
  420.          eav = ebase = expand(av[ac], &eac);
  421.          inc = 0;
  422.          if (eav) {
  423.             if (ac + eac + 2 > MAXAV) {
  424.                ierror (NULL, 506);
  425.                err = 1;
  426.             } else {
  427.                QuickSort(eav, eac);
  428.                for (; eac; --eac, ++eav)
  429.                   av[ac++] = push_cpy(*eav);
  430.             }
  431.             free_expand (ebase);
  432.          }
  433.       }
  434.  
  435.       /* process redirection  */
  436.  
  437.       if (redir && !err) {
  438.          register char *file = (doexpand) ? av[--ac] : av[ac];
  439.  
  440.          if (redir < 0)
  441.             Cin_name = file;
  442.          else {
  443.             Cout_name = file;
  444.             Cout_append = (redir == 2);
  445.          }
  446.          inc = 0;
  447.       }
  448.  
  449.       /* check elast for space */
  450.  
  451.       if (inc) {
  452.          ++ac;
  453.          if (ac + 2 > MAXAV) {
  454.             ierror (NULL, 506);
  455.             err = 1;                /* error condition */
  456.             elast = 0;              /* don't process any more arguemnts */
  457.          }
  458.       }
  459.       if (elast == ' ')
  460.          goto step5;
  461.    }
  462. stepdone:
  463.    av[ac] = '\0';
  464.  
  465.    /* process pipes via files */
  466.  
  467.    if (elast == '|' && !err) {
  468.       static int which;             /* 0 or 1 in case of multiple pipes */
  469.       which = 1 - which;
  470.       Cout_name = (which) ? Pipe1 : Pipe2;
  471.       Cout_ispipe = 1;
  472.    }
  473.  
  474.  
  475.    if (err)
  476.       goto done0;
  477.  
  478.    {
  479.       register int i;
  480.       char save_elast;
  481.       char *compile_av();
  482.       register char *avline;
  483.       unsigned char delim = ' ';
  484.  
  485.       save_elast = elast;
  486.       if (pend_alias || (cmd_stat(command) & ST_AV))
  487.          delim = 0xA0;
  488.       avline = compile_av(av,((pend_alias) ? 1 : 0), ac , delim);
  489.  
  490.  
  491.       if (pend_alias) {                               /* special % alias */
  492.          register char *ptr, *scr;
  493.          for (ptr = pend_alias; *ptr && *ptr != ' '; ++ptr);
  494.          set_var (LEVEL_SET, pend_alias + 1, avline);
  495.          free (avline);
  496.  
  497.          scr = malloc((strlen(ptr) << 2) + 2);
  498.          preformat (ptr, scr);
  499.          fcomm (scr, 1);
  500.          unset_var (LEVEL_SET, pend_alias + 1);
  501.       } else {                                        /* normal command  */
  502.          register int ccno;
  503.          long  oldcin  = Myprocess->pr_CIS;
  504.          long  oldcout = Myprocess->pr_COS;
  505.          char *Cin_buf;
  506.          struct FileHandle *ci;
  507.          long oldbuf;
  508.  
  509.          fflush(stdout);
  510.          ccno = find_command (command);
  511.          if ((Command[ccno].stat & (ST_NORED | ST_COND)) == 0) {
  512.             if (Cin_name) {
  513.                if ((Cin = (long)extOpen(Cin_name,1005L)) == 0L) {
  514.                   ierror (NULL, 504);
  515.                   err = 1;
  516.                   Cin_name = '\0';
  517.                } else {
  518.                   Myprocess->pr_CIS = _devtab[stdin->_unit].fd = Cin;
  519.                   ci = (struct FileHandle *)(((long)Cin)<<2);
  520.                   Cin_buf = (char *)AllocMem(202L, MEMF_PUBLIC);
  521.                   oldbuf = ci->fh_Buf;
  522.                   if (ci->fh_Buf == 0) /* fexec expects a CIS buffer */
  523.                      ci->fh_Buf = (long)Cin_buf>>2;
  524.                }
  525.             }
  526.             if (Cout_name) {
  527.                if (Cout_append && (Cout =(long)extOpen(Cout_name, 1005L)) ) {
  528.                      Seek(Cout, 0L, 1L);
  529.                } else {
  530.                   Cout = (long)extOpen(Cout_name,1006L);
  531.                }
  532.                if (Cout == NULL) {
  533.                   err = 1;
  534.                   ierror (NULL, 504);
  535.                   Cout_name = '\0';
  536.                   Cout_append = 0;
  537.                } else {
  538.                   Myprocess->pr_COS = _devtab[stdout->_unit].fd = Cout;
  539.                }
  540.             }
  541.          }
  542.          if (ac < Command[ccno].minargs + 1) {
  543.             ierror (NULL, 500);
  544.             err = -1;
  545.          } else if (!err) {
  546.             i = (*Command[ccno].func)(avline, Command[ccno].val);
  547.             if (i < 0)
  548.                i = 20;
  549.             err = i;
  550.          }
  551.          free (avline);
  552.          if (E_stack == 0 && Lastresult != err) {
  553.             Lastresult = err;
  554.             seterr();
  555.          }
  556.          if ((Command[ccno].stat & (ST_NORED | ST_COND)) == 0) {
  557.             if (Cin_name) {
  558.                fflush(stdin);
  559.                clearerr(stdin);
  560.                ci->fh_Buf = oldbuf;
  561.                extClose(Cin);
  562.                FreeMem(Cin_buf, 202L);
  563.             }
  564.             if (Cout_name) {
  565.                fflush(stdout);
  566.                clearerr(stdout);
  567.                stdout->_flags &= ~_DIRTY;    /* because of nil: device */
  568.                extClose(Cout);
  569.                Cout_append = 0;
  570.             }
  571.          }
  572.          Myprocess->pr_CIS =  _devtab[stdin->_unit].fd  = oldcin;
  573.          Myprocess->pr_COS =  _devtab[stdout->_unit].fd = oldcout;
  574.       }
  575.  
  576.       if (Cin_ispipe && Cin_name)
  577.          DeleteFile(Cin_name);
  578.       if (Cout_ispipe) {
  579.          Cin_name = Cout_name;         /* ok to assign.. static name */
  580.          Cin_ispipe = 1;
  581.       } else {
  582.          Cin_name = '\0';
  583.       }
  584.       Cout_name = '\0';
  585.       Cout_ispipe = 0;
  586.       elast = save_elast;
  587.    }
  588.    mpop_tobase();                      /* free arguments   */
  589.    mpush_base();                       /* push dummy base  */
  590.  
  591. done0:
  592.    {
  593.       char *str;
  594.       if (err && E_stack == 0) {
  595.          str = get_var(LEVEL_SET, v_except);
  596.          if (err >= ((str)?atoi(str):1)) {
  597.             if (str) {
  598.                ++H_stack;
  599.                ++E_stack;
  600.                exec_command(str);
  601.                --E_stack;
  602.                --H_stack;
  603.             } else {
  604.                Exec_abortline = 1;
  605.             }
  606.          }
  607.       }
  608.       if (elast != 0 && Exec_abortline == 0)
  609.          err = fcomm(nextstr, 0);
  610.       Exec_abortline = 0;
  611.       if (Cin_name)
  612.          DeleteFile(Cin_name);
  613.       Cin_name = NULL;
  614.       Cin_ispipe = 0;
  615.    }
  616. done1:
  617.    mpop_tobase();
  618.    if (freeok)
  619.       free(str);
  620.    --alias_count;
  621.    return ((int)err);                  /* TRUE = error occured    */
  622. }
  623.  
  624.  
  625. char *
  626. exarg(ptr)
  627. unsigned char **ptr;
  628. {
  629.    register unsigned char *end;
  630.    register unsigned char *start;
  631.  
  632.    start = end = *ptr;
  633.    while (*end && *end != 0x80 && *end != ';' && *end != '|' && *end != ' ')
  634.       ++end;
  635.    elast = *end;
  636.    *end = '\0';
  637.    *ptr = end + 1;
  638.    return ((char *)start);
  639. }
  640.  
  641. static char **Mlist;
  642.  
  643. mpush_base()
  644. {
  645.    char *str;
  646.  
  647.    str = malloc(5);
  648.    *(char ***)str = Mlist;
  649.    str[4] = 0;
  650.    Mlist = (char **)str;
  651. }
  652.  
  653. char *
  654. mpush(bytes)
  655. {
  656.    char *str;
  657.  
  658.    str = malloc(6 + bytes + 2);   /* may need extra 2 bytes in do_run() */
  659.    *(char ***)str = Mlist;
  660.    str[4] = 1;
  661.    Mlist = (char **)str;
  662.    return (str + 5);
  663. }
  664.  
  665. mpop_tobase()
  666. {
  667.    register char *next;
  668.    while (Mlist) {
  669.       next = *Mlist;
  670.       if (((char *)Mlist)[4] == 0) {
  671.          free (Mlist);
  672.          Mlist = (char **)next;
  673.          break;
  674.       }
  675.       free (Mlist);
  676.       Mlist = (char **)next;
  677.    }
  678. }
  679.  
  680.  
  681. /*
  682.  * Insert 'from' string in front of 'str' while deleting the
  683.  * first entry in 'str'.  if freeok is set, then 'str' will be
  684.  * free'd
  685.  */
  686.  
  687. char *format_insert_string(str, from, freeok)
  688. char *str;
  689. char *from;
  690. int *freeok;
  691. {
  692. register char *new1, *new2;
  693. register unsigned char *strskip;
  694. int len;
  695.  
  696. for (strskip = (unsigned char *)str;
  697.         *strskip && *strskip != ' ' 
  698.         && *strskip != ';' && *strskip != '|'
  699.         && *strskip != 0x80; ++strskip);
  700. len = strlen(from);
  701. new1 = malloc((len << 2) + 2);
  702. preformat(from, new1);
  703. len = strlen(new1) + strlen(strskip);
  704. new2 = malloc(len+2);
  705. strcpy(new2, new1);
  706. strcat(new2, strskip);
  707. new2[len+1] = 0;
  708. free (new1);
  709. if (*freeok) free (str);
  710. *freeok = 1;
  711. return new2;
  712. }
  713.  
  714. cmd_stat(str)
  715. char *str;
  716. {
  717. return(Command[find_command(str)].stat);
  718. }
  719.  
  720. find_command(str)
  721. char *str;
  722. {
  723. register unsigned short i;
  724. int len = strlen(str);
  725.  
  726. for (i = 0; Command[i].func; ++i)
  727.     if ( ! strncmp(str, Command[i].name, len)) return (int)i;
  728. return 0;
  729. }
  730.  
  731. do_help()
  732. {
  733. register struct COMMAND *com;
  734. int i=0;
  735.  
  736. for (com = &Command[1]; com->func; ++com) {
  737.     printf ("%-12s", com->name);
  738.     if (++i % 6 == 0) printf("\n");
  739.     }
  740. printf("\n");
  741. return 0;
  742. }
  743.  
  744. char *push_cpy(s)
  745. char *s;
  746. {
  747. return strcpy(mpush(strlen(s)), s);
  748. }
  749.